/*
 * PmwDrv.c
 *
 *  Created on: Apr 14, 2015
 *      Author: mxw
 */
#include "PmwDrv.h"
#include <S3C2440addr.h>

void init_pmw() {
    rGPBCON &= ~3;          //set GPB0 as tout0, pwm output
    rGPBCON |= 2;

    rTCFG0 &= ~0xff;
    rTCFG0 |= 15;           //prescaler0 = 15+1

    rTCFG1 &= ~0xf;
    rTCFG1 |= 2;            //mux = 1/8
//    rTCNTB0 = (PCLK>>7)/262;
    rTCMPB0 = rTCNTB0>>1;   // 50%
    rTCON &= ~0x1f;
    rTCON |= 0xa;           //disable deadzone, auto-reload, inv-off, update TCNTB0&TCMPB0, stop timer 0
}

void start_sing() {
    rTCON |= 0x1;           //start timer 0
    rTCON &= ~2;            //clear manual update bit
}

void set_freq(int freq) {
 //   rTCNTB0 = (PCLK>>7)/freq;
    rTCMPB0 = rTCNTB0 >> 1;   // 50%
    rTCON |= 0xa;           //disable deadzone, auto-reload, inv-off, update TCNTB0&TCMPB0, stop timer 0
}

void stop_sing() {
//    rGPBCON &= ~3;          //set GPB0 as gerneral output
//    rGPBCON |= 1;
//    rGPBDAT &= ~1;

    rTCON &= ~0x1;           //stop timer 0
    rTCON &= ~2;            //clear manual update bit
}

